﻿Imports LecturerAppointmentSystem.callback
Imports System.Net
Imports LecturerAppointmentSystem.UserDetails
Imports System.Data.OleDb

Public Class Login
    Inherits System.Web.UI.Page

    Dim facebookId As String = ""
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Request.IsAuthenticated AndAlso Not String.IsNullOrEmpty(Request.QueryString("ReturnUrl")) Then
                Response.Redirect("Errors.aspx")
            End If
        End If
        If Not String.IsNullOrEmpty(Request.QueryString("notApproved")) Then

        End If
        'Response.Redirect("callback.aspx")
        'If Request.Cookies("fbs_" + FaceBookAppKey) Is Nothing Then
        '    fblogin.Visible = True
        'Else
        '    'Get user's facebook id
        '    Dim cookie As String = Request.Cookies("fbs_" + FaceBookAppKey).Value
        '    cookie = cookie.Replace("""", "")
        '    Dim facebookValues As NameValueCollection = HttpUtility.ParseQueryString(cookie)
        '    Dim fbJson As String = GetFacebookUserJSON(facebookValues("uid"), facebookValues("access_token"))
        '    Dim jsonHash As Hashtable = DirectCast(JSON.JsonDecode(fbJson), Hashtable)
        '    facebookId = TryCast(jsonHash("id"), String)
        '    Dim allUsers As Array = getAllUsersByFacebookId()
        '    If doesUserExist(allUsers, facebookId) Then
        '        Dim allRegisteredUsers As Array = getAllUsersByFacebookId()
        '        Dim userName As String = getUserName(allRegisteredUsers, facebookId)
        '        FormsAuthentication.RedirectFromLoginPage(userName, True)
        '        Response.Redirect("Default.aspx?fbid=" & facebookId)
        '    Else
        '        Response.Redirect("Register.aspx?fbid=" & facebookId)
        '    End If
        'End If
    End Sub

    Sub deleteUser(ByVal userId As Integer)
        Dim sqlDeleteMembership As String = "DELETE * FROM aspnet_Membership WHERE UserId = @UserId"
        Dim cmdDeleteMembership As OleDbCommand = New OleDbCommand(sqlDeleteMembership, conn)
        cmdDeleteMembership.Parameters.AddWithValue("@UserId", userId)
        Dim sqlDeleteUser As String = "DELETE * FROM aspnet_Users WHERE UserId = @UserId"
        Dim cmdDeleteUser As OleDbCommand = New OleDbCommand(sqlDeleteUser, conn)
        cmdDeleteUser.Parameters.AddWithValue("@UserId", userId)
        Dim roleId As Integer
        Dim getRoleSql As String = "SELECT RoleId FROM aspnet_UsersInRoles WHERE UserId = @UserId"
        Dim getRoleCmd As OleDbCommand = New OleDbCommand(getRoleSql, conn)
        getRoleCmd.Parameters.AddWithValue("@UserId", userId)
        Dim reader As OleDbDataReader = getRoleCmd.ExecuteReader()
        If reader.Read() Then
            roleId = CType(reader("RoleId"), Integer)
        End If
        Dim sqlDeleteRole As String = "DELETE * FROM aspnet_UsersInRoles WHERE UserId = @UserId"
        Dim cmdDeleteRole As OleDbCommand = New OleDbCommand(sqlDeleteRole, conn)
        cmdDeleteRole.Parameters.AddWithValue("@UserId", userId)
        Dim sqlDeleteStudent As String = "DELETE * FROM Student WHERE Fkid_UserId = @UserId"
        Dim cmdDeleteStudent As OleDbCommand = New OleDbCommand(sqlDeleteStudent, conn)
        cmdDeleteStudent.Parameters.AddWithValue("@Fkid_UserId", userId)
        Dim sqlDeleteLecturer As String = "DELETE * FROM Lecturer WHERE Fkid_UserId = @UserId"
        Dim cmdDeleteLecturer As OleDbCommand = New OleDbCommand(sqlDeleteLecturer, conn)
        cmdDeleteLecturer.Parameters.AddWithValue("@Fkid_UserId", userId)
        Dim sqlDeleteFbId As String = "DELETE * FROM UserFacebookIdJunction WHERE UserId = @UserId"
        Dim cmdDeleteFbId As OleDbCommand = New OleDbCommand(sqlDeleteFbId, conn)
        cmdDeleteFbId.Parameters.AddWithValue("@UserId", userId)
        conn.Open()
        cmdDeleteMembership.ExecuteNonQuery()
        cmdDeleteUser.ExecuteNonQuery()
        cmdDeleteRole.ExecuteNonQuery()
        If roleId = 3 Then
            cmdDeleteStudent.ExecuteNonQuery()
        ElseIf roleId = 2 Then
            cmdDeleteLecturer.ExecuteNonQuery()
        End If
        cmdDeleteFbId.ExecuteNonQuery()
        conn.Close()
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        deleteUser(CType(TextBox1.Text, Integer))
    End Sub
End Class